﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ThreadsTest
{
    public partial class Form1 : Form
    {
        private static readonly object Form_Lock = new object();
        private static Queue<long> queue = new Queue<long>();
        private static Queue<long> queue1 = new Queue<long>();
        public Form1()
        {
            InitializeComponent();
        }
        //测试局部变量和成员变量之间相同名称时会有什么情况发生。（编译器看到的是假象，实际局部变量不会影响成员变量。）
        int k = 10;
        int F()
        {
            return k;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            {
                for (int i = 0; i < 5; i++)
                {
                    //这个地方将i赋值给k使得K变成了一个闭包。
                    int k = i;
                    Console.WriteLine(k);
                    Console.WriteLine(F());
                    Task.Factory.StartNew(() => BingFaCeShi(k));
                }
            }
            //{
            //    Task<int> task = Task.Delay(new TimeSpan(0, 0, 2)).ContinueWith<int>((t) =>
            //    {
            //        Console.WriteLine("测试一下Task.Delay是否阻塞主线程。。。。");
            //        return 3;
            //    });
            //    int i = task.Result;
            //    Task.WaitAny();
            //    Console.WriteLine("主线程立刻执行。。。。");
            //}
            Console.ReadKey();
        }

        private void DoTest()
        {
            lock (this)
            {

            }
        }

        private void BingFaCeShi(int i)
        {
            //Task<int> task = Task.Delay(new TimeSpan(0, 0, 2)).ContinueWith<int>((t) =>
            //{
            //    Thread.Sleep(2000);
            //    Console.WriteLine($"测试一下并发功能，此次调用的是---第{i}---人。。。。");
            //    return 3;
            //});
            //int result = task.Result;
            //Console.WriteLine($"第---{i}人---执行完毕。。。。");

            Console.WriteLine($"测试一下并发功能，此次调用的是---第{i}---人。。。。");
            Thread.Sleep(2000);
            Console.WriteLine($"第---{i}人---执行完毕。。。。");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            while (true)
            {

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(Run);
            thread.IsBackground = true;
            thread.Start();

            Thread thread1 = new Thread(Run);
            thread1.IsBackground = true;
            thread1.Start();

            Thread thread2 = new Thread(Run);
            thread2.IsBackground = true;
            thread2.Start();

            Thread thread3 = new Thread(Run);
            thread3.IsBackground = true;
            thread3.Start();

            Thread thread4 = new Thread(Run);
            thread4.IsBackground = true;
            thread4.Start();
            Thread thread5 = new Thread(Run);
            thread5.IsBackground = true;
            thread5.Start();

            Thread thread6 = new Thread(Run);
            thread6.IsBackground = true;
            thread6.Start();
        }

        private void Run()
        {
            while (true)
            {

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            queue = new Queue<long>();
            Stopwatch sw = new Stopwatch();
            sw.Start();

            for(int i = 0; i <= 100000000; i++)
            {
                queue.Enqueue(i);
                queue1.Enqueue(i);
            }
            sw.Stop();
            var time = sw.ElapsedMilliseconds.ToString();
            label2.Text = time;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            DeQueue(queue);
            DeQueue(queue1);


            sw.Stop();
            var time = sw.ElapsedMilliseconds.ToString();
            label5.Text = time;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            var thread1 = new Thread(()=> DeQueue(queue));
            thread1.IsBackground = true;
            thread1.Start();

            Thread thread2 = new Thread(() => DeQueue(queue1));
            thread2.IsBackground = true;
            thread2.Start();
            thread1.Join();
            thread2.Join();
            sw.Stop();
            var time = sw.ElapsedMilliseconds.ToString();
            label6.Text = time;
        }

        private void DeQueue(Queue<long> queue)
        {
            while (queue.Count > 0)
            {
                queue.Dequeue();
            }
        }
    }
}
